﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UMLDesigner
{
    partial class FormControl : UserControl
    {
        private ClassView editionClass;
        private ClassData classData;
        private List<AttributeData> temporalAttributes=new List<AttributeData>();
        private List<MethodData> temporalMethods = new List<MethodData>();

        public FormControl()
        {
            InitializeComponent();
            comboVisibility.DataSource = VisibilityType.GetTypes();
            comboModifiers.DataSource = ModifiersTypes.GetTypes();
        }

        private void buttonAddAtributes_Click(object sender, EventArgs e)
        {
            AttributesWindow attribute = new AttributesWindow();
            attribute.Show();
        }

        private void btnModifyAttributes_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                int index = listBoxAttributes.SelectedIndex;
                AttributesWindow addAttributes = new AttributesWindow(temporalAttributes[index]);

                addAttributes.Show();
            }
        }

        private void btnAddMethods_Click(object sender, EventArgs e)
        {
            MethodsWindow anyadirMetodos = new MethodsWindow();
            anyadirMetodos.Show();
        }



        public void Refresh(ClassView classView)
        {
            editionClass = classView;
            classData = classView.GetClassData();

            txtClassName.Text =  classData.GetName();

            comboVisibility.SelectedItem = classData.GetVisibility();
            comboModifiers.SelectedItem = classData.GetModifier();

            listBoxAttributes.Items.Clear();
            foreach(AttributeData attr in classData.GetAttributes())
            {
                listBoxAttributes.Items.Add(attr.GetUMLRepresentation());
            }

            temporalAttributes.Clear();
            for (int i = 0; i < classData.GetAttributes().Count; i++)
            {
                temporalAttributes.Add(classData.GetAttributes()[i]);
            }

            listBoxMethods.Items.Clear();
            foreach (MethodData mtd in classData.GetMethods())
            {
                listBoxMethods.Items.Add(mtd.GetUMLRepresentation());
            }

            temporalMethods.Clear();
            for (int i = 0; i < classData.GetMethods().Count; i++)
            {
                temporalMethods.Add(classData.GetMethods()[i]);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (editionClass != null)
            {
                classData.SetName(txtClassName.Text);
                classData.SetAttributes(temporalAttributes);
                classData.SetMethods(temporalMethods);
                classData.SetModifier(comboModifiers.Text);
                classData.SetVisibility(comboVisibility.Text);

                editionClass.Refresh();
            }
        }

        public void Deselect()
        {
            txtClassName.Text = "";
            editionClass = null;
        }

       
        public void AddAttribute(string name,string type,string visibility)
        {
            AttributeData attribute = new AttributeData(name,type,visibility);
            listBoxAttributes.Items.Add(attribute.GetUMLRepresentation());
            temporalAttributes.Add(attribute);           
        }

        public void ModifyAttribute(string name, string type, string visibility)
        {
            int index = listBoxAttributes.SelectedIndex;
            temporalAttributes[index].SetName(name);
            temporalAttributes[index].SetType(type);
            temporalAttributes[index].SetVisibility(visibility);
            listBoxAttributes.Items[index] = temporalAttributes[index].GetUMLRepresentation();
        }

        private void btnDeleteAtrribute_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                 for (int i = 0; i < temporalAttributes.Count; i++)
                {
                
                    if (temporalAttributes[i].GetUMLRepresentation() == listBoxAttributes.Text)
                    {
                        temporalAttributes.Remove(temporalAttributes[i]);
                    }
                }
              
                listBoxAttributes.Items.Remove(listBoxAttributes.SelectedItem);
                
            }
        }

        public void AddMethods(string visibilidad, string nombre,List<ParameterData>parametros,string retorno)
        {
            MethodData metodo = new MethodData(visibilidad, nombre, retorno);
            metodo.SetParameters(parametros);
            listBoxMethods.Items.Add(metodo.GetUMLRepresentation());
            temporalMethods.Add(metodo);
        }
        public void EditMethod(string visibilidad, string nombre, List<ParameterData> parametros, string retorno)
        {
            int indice = listBoxMethods.SelectedIndex;
            temporalMethods[indice].SetName(nombre);
            temporalMethods[indice].SetReturnType(retorno);
            temporalMethods[indice].SetParameters(parametros);
            temporalMethods[indice].SetVisibility(visibilidad);
            listBoxMethods.Items[indice] = temporalMethods[indice].GetUMLRepresentation();
        }

        private void btnDeleteMethod_Click(object sender, EventArgs e)
        {
            if (listBoxMethods.SelectedItem != null)
            {
                for (int i = 0; i < temporalMethods.Count; i++)
                {
                    if (temporalMethods[i].GetUMLRepresentation() == listBoxMethods.Text)
                    {
                        temporalMethods.Remove(temporalMethods[i]);
                    }
                }

                listBoxMethods.Items.Remove(listBoxMethods.SelectedItem);
            }
        }

        private void btnEditMethod_Click(object sender, EventArgs e)
        {
            if (listBoxMethods.SelectedItem != null)
            {
                int index = listBoxMethods.SelectedIndex;
                MethodsWindow editMethod = new MethodsWindow(temporalMethods[index]);
                editMethod.Show();
            }
        }

    }
}
